package workexp.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

/**
 * @author zhukai
 * 
 */
public class retryListener implements ConnectionStateListener{

	/* (non-Javadoc)
	 * @see org.apache.curator.framework.state.ConnectionStateListener#stateChanged(org.apache.curator.framework.CuratorFramework, org.apache.curator.framework.state.ConnectionState)
	 */
	@Override
	public void stateChanged(CuratorFramework client, ConnectionState newState) {
		System.out.println(newState.name());
		if(newState == ConnectionState.RECONNECTED) {
			Stat state;
			try {
				state = client.checkExists().forPath("/testtmp/server1");
				if(null==state) {
					client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/testtmp/server1","server1 reconnect".getBytes());
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
